package com.wondersgroup.insurance.dnuilearn.views.path;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2017/5/24.
 */

public class WaterLoadView extends View {

    private static final int INT_WAVE_LENGTH = 1000;
    final int halfWaveLength = INT_WAVE_LENGTH/2;
    Paint mPaint;
    Path mPath =null;


    float ratio=0;

    int step;
    private int mDeltaX;


    public WaterLoadView(Context context) {
        this(context, null);
    }

    public WaterLoadView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaterLoadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        handler.sendEmptyMessageDelayed(100,3000);
    }

    void init(){
        mPaint= new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(6);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPath = new Path();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawColor(Color.WHITE, PorterDuff.Mode.CLEAR);



        mPath.reset();

        mPath.moveTo(-INT_WAVE_LENGTH + mDeltaX,getHeight()* (1-ratio));
        for(int i = -INT_WAVE_LENGTH ; i < getWidth() + INT_WAVE_LENGTH;
            i += INT_WAVE_LENGTH){
            mPath.rQuadTo(halfWaveLength/2,60,halfWaveLength,0);
            mPath.rQuadTo(halfWaveLength/2,-60,halfWaveLength,0);
        }


//        mPath.lineTo(getWidth(),getHeight()* (1-ratio));
        mPath.lineTo(getWidth(),getHeight());
        mPath.lineTo(0,getHeight());
        mPath.close();
        canvas.drawPath(mPath,mPaint);
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            startAnimation();
        }
    };
    public void startAnimation(){
        ValueAnimator valueAnimator = null;
        if (step == 0){
            valueAnimator = ValueAnimator.ofInt(0,100);
        }else {
            valueAnimator = ValueAnimator.ofInt(100,0);

        }

        valueAnimator.setDuration(8000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                step  = (int) animation.getAnimatedValue();
                ratio = step/100.0f;
                mDeltaX = (int) (INT_WAVE_LENGTH*ratio);
                invalidate();
            }


        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                handler.sendEmptyMessageDelayed(100,2000);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        valueAnimator.start();
    }


}
